/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commonClasses.Messages;

import commonClasses.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import commonClasses.commonInterfaces.ProtocolIntf;

/**
 *
 * @author MGL
 */
public class Msg {

    private long _id = -1;
    private ProtocolIntf _protocol;
    private static long _counter = 0;

    public static long receiveMsgId(DataInputStream dis) throws Exception {

        int b1 = dis.readUnsignedByte();
        int b2 = dis.readUnsignedByte();
        int b3 = dis.readUnsignedByte();
        int b4 = dis.readUnsignedByte();

        return (long) (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
    }

    public int receive8uint() {

        try {
            DataInputStream dis = _protocol.getDIS();
            int b1 = dis.readUnsignedByte();
            return (int) (b1);
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public int receive16uint() {

        try {
            DataInputStream dis = _protocol.getDIS();
            int b1 = dis.readByte();// readUnsignedByte();
            int b2 = dis.readByte();//readUnsignedByte();
            return (int) (b1 << 8) | (b2);
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public long receive32uint() {

        try {
            DataInputStream dis = _protocol.getDIS();
            int b1 = dis.readUnsignedByte();
            int b2 = dis.readUnsignedByte();
            int b3 = dis.readUnsignedByte();
            int b4 = dis.readUnsignedByte();

            return (long) (b1 << 24) | (b2 << 16) | (b3 << 8) | (b4);
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public static long getCounter() {
        return _counter++;
    }

    public Msg(ProtocolIntf protocol, long id) {
        this._protocol = protocol;
        this._id = id;
    }

    public char[] stringToCharArr(String s) {
        return s.toCharArray();
    }

    public byte[] receiveByteArr(int length) {
        try {
            DataInputStream dis = _protocol.getDIS();
            byte b[] = new byte[length];

            int poz = 0;
            int i = 0;
            do {
                poz += i;
                i = dis.read(b, poz, length - poz);
                Utils.write("receiveByteArr: wczytałem tyle bajtow(..): " + (poz + i));
            } while (i >= 0 && poz + i < length);

            Utils.write("receiveByteArr: wczytałem tyle bajtow(..): " + (poz + i));
            return b;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void sendByteArr(byte arr[]) {

        try {
            DataOutputStream dos = _protocol.getDOS();
            dos.write(arr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public char[] receiveCharArr(int length) {
        try {
            DataInputStream dis = _protocol.getDIS();
            char cArr[] = new char[length];
            for (int i = 0; i < length; i++) {
                cArr[i] = dis.readChar();
                Utils.write("wczytalem:" + cArr[i]);
            }
            Utils.write("(wychodzę)wczytalem:" + cArr.toString());

            return cArr;
        } catch (IOException e) {
            e.printStackTrace();
            return new char[0];
        }

    }

    public static int longToInt(long lng) {
        return Integer.valueOf(Long.valueOf(lng).toString());
    }

    public static long receiveUint(ProtocolIntf protocol) { //statyczna
        DataInputStream dis = protocol.getDIS();
        try {

            int arr[] = new int[4];
            for (int i = 0; i < 4; i++) {
                arr[i] = dis.readUnsignedByte();
            }
            return intArrToLong(arr);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return 0;
    }

    public long receiveUint() { //do użyku wewnąć klasy rozszerzającej MSG
        DataInputStream dis = _protocol.getDIS();
        try {

            int arr[] = new int[4];
            for (int i = 0; i < 4; i++) {
                arr[i] = dis.readUnsignedByte();
            }
            return intArrToLong(arr);
        } catch (IOException e) {
            e.printStackTrace();
        }


        return 0;
    }

    public void sendIntArr(int arr[]) throws Exception {
        DataOutputStream dos = this.getProtocol().getDOS();

        for (int i = 0; i < arr.length; i++) {
            dos.write(arr[i]);

        }
    }

    public void sendCharArr(char arr[]) throws Exception {
        DataOutputStream dos = this.getProtocol().getDOS();

        dos.writeChar(arr[0]);
        dos.writeChar(arr[1]);
        dos.writeChar(arr[2]);
    }

    public void sendCharArr(char arr[], int length) throws Exception {
        DataOutputStream dos = this.getProtocol().getDOS();

        for (int i = 0; i < length; i++) {
            dos.writeChar(arr[i]);
        }
    }

    public int[] longToArr(long nr) throws Exception {

        int arr[] = new int[4];

        arr[0] = Integer.parseInt(Long.valueOf((nr >> 24) & 0xff).toString());
        arr[1] = Integer.parseInt(Long.valueOf((nr >> 16) & 0xff).toString());
        arr[2] = Integer.parseInt(Long.valueOf((nr >> 8) & 0xff).toString());
        arr[3] = Integer.parseInt(Long.valueOf((nr) & 0xff).toString());

        return arr;
    }

    public int[] intToArr(int nr) throws Exception {

        int arr[] = new int[2];

        arr[0] = Integer.parseInt(Long.valueOf((nr >> 8) & 0xff).toString());
        arr[1] = Integer.parseInt(Long.valueOf((nr) & 0xff).toString());

        return arr;
    }

    public int[] idToIntArr() throws Exception {

        if (_id == -1) {
            throw new Exception("_id not initialized!");
        }

        return longToArr(_id);
    }

    public static long intArrToLong(int arr[]) {
        return (long) (arr[0] << 24) | (arr[1] << 16) | (arr[2] << 8) | (arr[3]);
    }

    public static long intArrToID(int arr[]) {
        return intArrToLong(arr);
    }

    public long getId() {
        return _id;
    }

    public ProtocolIntf getProtocol() {
        return _protocol;
    }

    public static void main(String args[]) {
        try {
            Msg msg = new Msg(null, 666);
            msg._id = 123456789;
            System.out.println(
                    Msg.intArrToID(msg.idToIntArr()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
